package com.liusand.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.liusand.common.Result;
import com.liusand.common.ThreadLocalUtils;
import com.liusand.pojo.PageInfo;
import com.liusand.service.EmployeeService;
import com.liusand.pojo.Employee;
import com.liusand.vo.EmployeeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :AdminController
 * creationTime :2022/11/20 17:39
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    public Result login(@RequestBody Employee employee){
        if(employee == null){
            return Result.error("登录失败");
        }
        EmployeeVo vo = employeeService.findByUsernameAndPassword(employee);
        if(vo == null){
            return Result.error("登录失败");
        }

        return Result.success(vo);
    }


    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");

//        boolean re = employeeService.clearToken(token);
//        if(!re){
//            return Result.error("退出失败");
//        }
        boolean re = redisTemplate.delete("employee_token:" + token);
        if(!re){
            return Result.error("退出失败");
        }
        return Result.success("退出登录成功");
    }

    @GetMapping("/page")
    public Result page(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Employee> info = employeeService.page(name, page, pageSize);
        return Result.success(info);
    }

    @GetMapping("/{id}")
    public Result findEmployee(@PathVariable String id){
        if(!StringUtils.hasText(id)){
            return Result.error("查询失败");
        }
        Employee employee = employeeService.findEmployeeById(id);
        return Result.success(employee);
    }

    @PostMapping
    public Result addEmployee(@RequestBody Employee employee){
        if(employee == null){
            return Result.error("员工新增失败");
        }
        boolean re = employeeService.saveEmployee(employee);
        if(!re){
            return Result.error("员工新增失败");
        }
        return Result.success("员工新增成功");
    }


    @PutMapping
    public Result updateEmployee(@RequestBody Employee employee){
        if(employee == null){
            return Result.error("员工信息修改失败");
        }
        boolean re = employeeService.updateEmployee(employee);
        if(!re){
            return Result.error("员工信息修改失败");
        }
        return Result.success("员工信息修改成功");
    }

}
